<?php

namespace frontend\modules\Api\controllers;

use common\services\TallyPmService;
use common\services\TallyRecordService;
use common\services\TallyStatReportServices;
use common\services\ToolService;
use Yii;

class TallyStatReportController extends ApiController
{



    public function actionRecordCountAnalysis(){
        $condition = Yii::$app->getRequest()->post();
        $resultData = TallyStatReportServices::record_count_analysis($condition);
        return self::successResponse($resultData);
    }



    public function actionCountDebtAnalysis(){
        $condition = Yii::$app->getRequest()->post();
        $tallyId = Yii::$app->getRequest()->post('tally_id');
        if(!$tallyId){
            return self::failureResponse(500,'账本id不能为空');
        }
        $uid = TallyPmService::getRoleUid($this->_user['id'],$tallyId);
        $return = [];
        $retData  = TallyRecordService::countDebt($condition,$uid);
        $return['in_format']  = $retData['in'];
        $return['out_format']  = $retData['out'];
        $return['debt_format']  = $retData['debt'];
        $resultData = TallyStatReportServices::count_debt_analysis($condition);
        $return['yfs']  = $resultData['inRemain'];
        $return['yff']  = $resultData['outRemain'];
        $return['hdebt']  = $resultData['debt']+$retData['debt'];
        return self::successResponse($return);
    }


    public function actionGuaCountDebtRean(){
        $condition = Yii::$app->getRequest()->get();
        $tallyId = Yii::$app->getRequest()->get('tally_id');
        if(!$tallyId){
            return self::failureResponse(500,'账本id不能为空');
        }
        $resultData = TallyStatReportServices::gua_count_debt_rean($condition);
        return  self::successResponse($resultData);
    }

    public function actionFutureCountDebtRean(){
        $condition = Yii::$app->getRequest()->get();
        $tallyId = Yii::$app->getRequest()->get('tally_id');
        if(!$tallyId){
            return self::failureResponse(500,'账本id不能为空');
        }
        $resultData = TallyStatReportServices::future_count_debt_rean($condition);
        return  self::successResponse($resultData);
    }




    public function actionExport(){
        $tallyId = Yii::$app->getRequest()->get('tally_id');
        if(!$tallyId){
            return self::failureResponse(500,'账本id不能为空');
        }
        $condition = Yii::$app->request->get(); // type 等参数
        //0收入流水1收入列表2支出流水3支出列表
        $export_type = Yii::$app->getRequest()->get('export_type',0);
        $type = Yii::$app->getRequest()->get('type',0);
        if($export_type == 1 && $type == 0){
            $condition['type'] = 10;
            $filePath = TallyStatReportServices::exportRecordList($export_type,$condition);
        }else if($export_type == 2 && $type == 0){
            $condition['type'] = 20;
            $filePath = TallyStatReportServices::exportRecordList($export_type,$condition);
        }else if($export_type == 1 && $type == 1){
            $condition['type'] = 10;
            $filePath = TallyStatReportServices::exportRecordAnalysis($export_type,$condition);
        }else if($export_type == 2 && $type == 1){
            $condition['type'] = 20;
            $filePath = TallyStatReportServices::exportRecordAnalysis($export_type,$condition);
        }else if($export_type == 3 && $type == 0){
            //挂账流水（应收应付）
            $filePath = TallyStatReportServices::exportArapList($condition);
        }else if($export_type == 4 && $type == 0){
            //挂账流水（应收应付）
            $filePath = TallyStatReportServices::exportFutureAnalysis($condition);
        }else if($export_type == 3 && $type == 1){
            //挂账流水（应收应付）
            $filePath = TallyStatReportServices::exportArapAnalysis($condition,0);
        }else if($export_type == 4 && $type == 1){
            //挂账流水（应收应付）
            $filePath = TallyStatReportServices::exportArapAnalysis($condition,1);
        }
        return self::successResponse(['filepath'=>$filePath]);
    }


    public function actionGuaCountAnslysis(){
        $tallyId = Yii::$app->getRequest()->post('tally_id');
        if(!$tallyId){
            return self::failureResponse(500,'账本id不能为空');
        }
        $condition = Yii::$app->request->post(); // type 等参数
        $retData = TallyStatReportServices::count_gua_anslysis($condition);
        return  self::successResponse($retData);
    }

}